import paramiko

# VPS主机信息
hostname = "38.141.14.105"
port = 22122
username = "root"
password = "junetiger"

# 创建SSH客户端
ssh = paramiko.SSHClient()
# 自动添加主机的密钥 (不安全, 建议只在可信的网络环境下使用)
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

try:
    # 连接到VPS
    ssh.connect(hostname, port, username, password)
    print(f"成功连接到 {hostname}")

    # 执行命令
    stdin, stdout, stderr = ssh.exec_command("ss -luntp")

    # 获取命令输出
    output = stdout.read().decode()
    error = stderr.read().decode()

    # 显示输出
    if output:
        print(f"命令输出:\n{output}")
    if error:
        print(f"命令错误:\n{error}")

except Exception as e:
    print(f"连接或执行命令时发生错误: {e}")

finally:
    # 关闭SSH连接
    ssh.close()
    print("SSH连接已关闭")